package com.stupro.cjlu.service;

//import com.stupro.cjlu.common.untils.ShiroUntils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stupro.cjlu.common.vo.ResultVO;
import com.stupro.cjlu.common.vo.StudentCourseItemVO;
import com.stupro.cjlu.dao.CourseDAO;
import com.stupro.cjlu.dao.StudentCourseDAO;
import com.stupro.cjlu.entity.Course;
import com.stupro.cjlu.entity.Studentcourse;
import com.stupro.cjlu.entity.Students;
import com.stupro.cjlu.manager.StudentsManager;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2021-05-10
 */
@Service
public class StudentsService extends BaseService {
    private final StudentsManager manager;
    private final UserService userService;
    private final StudentCourseDAO studentCourseDAO;
    private final CourseDAO courseDAO;

    public StudentsService(StudentsManager manager, UserService userService,StudentCourseDAO studentCourseDAO,
                           CourseDAO courseDAO) {
        this.manager = manager;
        this.userService = userService;
        this.studentCourseDAO = studentCourseDAO;
        this.courseDAO = courseDAO;
    }

    //获取成绩
    public ResultVO getCourse(Integer page,String name,String className,String courseName){
        IPage<StudentCourseItemVO> data = studentCourseDAO.getCourse(page,className,courseName,name);
        ResultVO resultVO = result(data.getRecords());

        resultVO.setTotal(data.getTotal());
        return resultVO;
    }
    //获取成绩
    public ResultVO getCourse(Integer page,String name,String className,String courseName,int teacherID){
        IPage<StudentCourseItemVO> data = studentCourseDAO.getCourse(page,className,courseName,name,teacherID);
        ResultVO resultVO = result(data.getRecords());

        resultVO.setTotal(data.getTotal());
        return resultVO;
    }

    public ResultVO getAll(Integer index){
        return result(manager.getAll(index));
    }

    public ResultVO getPageCount(String className, String name) {
        return result(manager.getPageCount(className, name));
    }

    public ResultVO getPage(Integer index, String className, String name) {
        return result(manager.getPage(index, className, name));
    }

    public ResultVO get(Integer id) {
        Students students = manager.get(id);
        if (students == null) {
            return failedResult("学生Id: " + id + "不存在!");
        }

        students.setStuPwd("");

        return result(students);
    }

    public ResultVO update(Students entity) {
        Students origin = manager.get(entity.getStuID());
        if (origin == null) {
            return failedResult("学生Id: " + entity.getStuID() + "不存在!");
        }
        /*if (manager.getClassById(entity.getStuClass()) == null) {
            return failedResult("所属班级Id: " + entity.getClassId() + "不存在!");
        }*/

        //     if (entity.getPassword().equals("")) {
//        entity.setStuPwd(origin.getStuPwd());
        //     } else {
        //        entity.setPassword(userService.computePasswordHash(entity.getPassword()));
        //   }

        manager.update(entity);
        return result("更新成功");
    }

    public ResultVO delete(Integer id) {
        if (manager.get(id) == null) {
            return failedResult("学生Id: " + id + "不存在!");
        }
        if (manager.hasStudentCourse(id)) {
            return failedResult("此学生还有未退选课程");
        }

        manager.delete(id);
        return result("删除成功");
    }

    public ResultVO create(Students entity) {
        if (manager.get(entity.getStuID()) != null) {
            return failedResult("学生Id: " + entity.getStuID() + "已存在!");
        }
        /*if (manager.getClassById(entity.getClassId()) == null) {
            return failedResult("所属班级Id: " + entity.getClassId() + "不存在!");
        }*/

        manager.create(entity);
        return result("添加成功");
    }

    public ResultVO listName() {
        return result(manager.listName());
    }

    public ResultVO initPwd(int stuID, String name, String stuClass) {
//        Students  students = manager.
//        return result(manager.update());
        return null;
    }


    public ResultVO getGrade(int studentID){
        return result(studentCourseDAO.listStudentCourseSelected(studentID));
    }

    public ResultVO chooseCourse(int studentID, int courseID) {
        Studentcourse studentcourse = new Studentcourse(null,studentID,courseID,new BigDecimal(0));

        if(studentCourseDAO.getByCourseIdAndStudentId(courseID,studentID)!=null){
            return failedResult("请不要重复选课");
        }
        return result(studentCourseDAO.insert(studentcourse));
    }

    public ResultVO getSelectedCourse(int stuID){
        return  result(studentCourseDAO.listStudentCourseSelected2(stuID));
    }

    public ResultVO deleteByScId(Integer scId){
        return result(manager.deleteByScID(scId));
    }
}